package com.lv.util;

public class TwoPoint {
    /**
     * 计算地球上任意两点(经纬度)距离
     *
     * @param long1
     *            第一点经度
     * @param lat1
     *            第一点纬度
     * @param long2
     *            第二点经度
     * @param lat2
     *            第二点纬度
     * @return 返回距离 单位：米
     */
    public static double Distance(double long1, double lat1, double long2,
                                  double lat2) {
        double a, b, R;
        R = 6378137.0; // 地球半径
        lat1 = lat1 * Math.PI / 180.0;
        lat2 = lat2 * Math.PI / 180.0;
        a = lat1 - lat2;
        b = (long1 - long2) * Math.PI / 180.0;
        double d;
        double sa2, sb2;
        sa2 = Math.sin(a / 2.0);
        sb2 = Math.sin(b / 2.0);
        d = 2
                * R
                * Math.asin(Math.sqrt(sa2 * sa2 + Math.cos(lat1)
                * Math.cos(lat2) * sb2 * sb2));
        return d;
    }

    // ==================================================


    public static void main(String[] args) {




        double d1 = Distance(123.4306, 41.70032, 123.4262, 41.70221);
        double d2 = Distance(123.4306, 41.70032, 123.4262, 41.70220);

        System.out.println(d1);
        System.out.println(d2);
        if (d1 > d2) {
            System.out.println(" d1 is ok! ");
        }
        double d3 = Distance(123.302706,  41.86669, 123.302706, 41.866678);
        System.out.println(d3);
    }

}

